var fs = require('fs')
var rp = require('request-promise')
var cheerio = require('cheerio')

var config = require('./config.js')
var util = require('./util/_default.js')
var xlsx = require('node-xlsx')

var helper = util.helper

var data = [['changeTimeStr', 'changeTimeStr2','convertF', 'createTimeStr', 'dirCode', 'dirName',
'drugWay', 'export', 'facName','limitedRange', 'modifyTimeStr', 'nameCn',
'normF', 'payPrice', 'saleLimit', 'selectedGoods', 'smlName',
'statusStr','syncProdId', 'totalCount', 'warpName','warpNameF']]



var pages =167

Begin();

function Begin() {
    console.log('---开始采集---');
    search(0);
    //gettoken('http://182.92.8.25:8110/WebResource.axd?d=MlL9CqcsojOwDUhkgKpr7ZVQjuPCZcxoNv6moSbxR9dzzxpV3YhUk-VKs2zX-GxQ3Q80XL-2Evv2raEOKQO3XEJgu901&t=635588912026805809')
}

//查询
function search(idx) {
    /* gettoken('http://182.92.8.25:8110/WebResource.axd?d=MlL9CqcsojOwDUhkgKpr7ZVQjuPCZcxoNv6moSbxR9dzzxpV3YhUk-VKs2zX-GxQ3Q80XL-2Evv2raEOKQO3XEJgu901&t=635588912026805809') */
    var opts = helper.deepCopy(config.reqOpts)
    opts.method = 'post'
    opts.url = 'http://fujian.udplat.com/index/comPubResultList'
    opts.headers.Referer='http://fujian.udplat.com/index/pubresultList'
    opts.form={
        draw:idx,
        start: idx * 100,
        length: 100,
        search: { value: '', regex: false },
        columns: [{
            data: 'dirCode', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'dirName', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'nameCn', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'drugWay', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'smlName', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'normF', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'convertF', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        },  {
            data: 'facName', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'selectedGoods', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'saleLimit', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'payPrice', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }, {
            data: 'limitedRange', name: '', searchable: true, orderable: false, search: { value: '', regex: false }
        }]
    }
    rp(opts).then(function (body) {
        var d = JSON.parse(body)
        console.log(idx+','+data.length)
        d.data.forEach(function (v, i, a) {
            data.push([
                v.changeTimeStr,v.changeTimeStr2,v.convertF,v.createTimeStr,v.dirCode,v.dirName,
                v.drugWay,v.export,v.facName,v.limitedRange,v.modifyTimeStr,v.nameCn,
                v.normF,v.payPrice,v.saleLimit,v.selectedGoods,v.smlName,
                v.statusStr,v.syncProdId,v.totalCount,v.warpName,v.warpNameF
            ])
        })
         if (idx + 1 < pages) {
             idx = idx+1
            search(idx)
        } else {
            saveExcel(data)
        } 
        //saveExcel(data)
    }).catch(function (err) {
        console.log('错误'+err.message)
    })

}

function saveExcel(data) {
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('result.xlsx', file, 'binary')
}

